package bean.managed;

import entity.MasterProductionScheduleWeeklyEntity;
import bean.managed.util.JsfUtil;
import bean.session.MasterProductionScheduleWeeklyEntityFacade;

import entity.MasterProductionScheduleDailyEntity;
import entity.MasterProductionScheduleMonthlyEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.SelectItem;
import managedbean.AbstractBacking;

@ManagedBean (name="masterProductionScheduleWeeklyEntityController")
@SessionScoped
public class MasterProductionScheduleWeeklyEntityController extends AbstractBacking implements Serializable {


    private MasterProductionScheduleWeeklyEntity current;
    private DataModel items = null;
    private DataModel createItems = null;
    @EJB private bean.session.MasterProductionScheduleWeeklyEntityFacade ejbFacade;
    @EJB 
    private bean.session.MasterProductionScheduleDailyEntityFacade dailyFacade;
    private long previousMonthOnHand = 0;
    private MasterProductionScheduleDailyEntity oneDay;

    public MasterProductionScheduleWeeklyEntityController() {
    }

    public MasterProductionScheduleWeeklyEntity getSelected() {
        if (current == null) {
            current = new MasterProductionScheduleWeeklyEntity();
        }
        return current;
    }

    private MasterProductionScheduleWeeklyEntityFacade getFacade() {
        return ejbFacade;
    }

    public String prepareList() {
        recreateModel();
        return "/entities/masterProductionScheduleWeeklyEntity/List?faces-redirect=true";
    }

    public String prepareCreate() {
        current = new MasterProductionScheduleWeeklyEntity();
        return "Create";
    }

    public String create() {
        try {
            current.created(getLoginPerson());
            getFacade().create(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("MasterProductionScheduleWeeklyEntityCreated"));
            return prepareCreate();
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }
    
    public String prepareView() {
        current = (MasterProductionScheduleWeeklyEntity)getItems().getRowData();
        setPreviousMonthOnHand(ejbFacade.getPreviousMonthOnHand(current));
        ArrayList<MasterProductionScheduleDailyEntity> dailyMPS = new ArrayList<MasterProductionScheduleDailyEntity>(current.getMpsDaily());
        MasterProductionScheduleMonthlyEntity mps = dailyMPS.get(0).getMpsMonthly();
        setCreateItems(getFacade().listCreateMPS(mps.getType(), current.getYearOfProduction(), current.getMonthOfProduction(), current.getProductionType()));
        setOneDay(dailyMPS.get(0));
        return "View";
    }

    public String prepareEdit() {
        current = (MasterProductionScheduleWeeklyEntity)getItems().getRowData();
        setPreviousMonthOnHand(ejbFacade.getPreviousMonthOnHand(current));
        ArrayList<MasterProductionScheduleDailyEntity> dailyMPS = new ArrayList<MasterProductionScheduleDailyEntity>(current.getMpsDaily());
        MasterProductionScheduleMonthlyEntity mps = dailyMPS.get(0).getMpsMonthly();
        setCreateItems(getFacade().listCreateMPS(mps.getType(), current.getYearOfProduction(), current.getMonthOfProduction(), current.getProductionType()));
        setOneDay(dailyMPS.get(0));
        return "Edit";
    }

    public String update() {
        try {
            current.modified(getLoginPerson());
            getFacade().edit(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("MasterProductionScheduleWeeklyEntityUpdated"));
            return "View";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String destroy() {
        current = (MasterProductionScheduleWeeklyEntity)getItems().getRowData();
        performDestroy();
        recreateModel();
        return "List";
    }

    public String destroyAndView() {
        performDestroy();
        recreateModel();
            return "List";
    }

    private void performDestroy() {
        try {
            getFacade().remove(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("MasterProductionScheduleWeeklyEntityDeleted"));
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
    }

    public DataModel getItems() {
        if (items == null) {
            items = getFacade().getAllWeeklyMps();
        }
        return items;
    }

    private void recreateModel() {
        items = null;
    }

    public SelectItem[] getItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
    }

    public long getPreviousMonthOnHand() {
        return previousMonthOnHand;
    }

    public void setPreviousMonthOnHand(long previousMonthOnHand) {
        this.previousMonthOnHand = previousMonthOnHand;
    }

    public DataModel getCreateItems() {
        return createItems;
    }

    public void setCreateItems(DataModel createItems) {
        this.createItems = createItems;
    }

    public MasterProductionScheduleDailyEntity getOneDay() {
        return oneDay;
    }

    public void setOneDay(MasterProductionScheduleDailyEntity oneDay) {
        this.oneDay = oneDay;
    }

    @FacesConverter(forClass=MasterProductionScheduleWeeklyEntity.class)
    public static class MasterProductionScheduleWeeklyEntityControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            MasterProductionScheduleWeeklyEntityController controller = (MasterProductionScheduleWeeklyEntityController)facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "masterProductionScheduleWeeklyEntityController");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.Long getKey(String value) {
            java.lang.Long key;
            key = Long.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Long value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof MasterProductionScheduleWeeklyEntity) {
                MasterProductionScheduleWeeklyEntity o = (MasterProductionScheduleWeeklyEntity) object;
                return getStringKey(o.getId());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: "+MasterProductionScheduleWeeklyEntityController.class.getName());
            }
        }

    }
    
    public ArrayList<MasterProductionScheduleWeeklyEntity> weeklyMPSForMonth (MasterProductionScheduleMonthlyEntity monthlyMPS) {
        return ejbFacade.getWeeklyMPSForMonth(monthlyMPS.getMonthOfProduction(), monthlyMPS.getYearOfProduction(), monthlyMPS.getProductionType());
    }
    
    public ArrayList<MasterProductionScheduleWeeklyEntity> weeklyMPS (MasterProductionScheduleWeeklyEntity weeklyMPS) {
        return ejbFacade.getWeeklyMPSForMonth(weeklyMPS.getMonthOfProduction(), weeklyMPS.getYearOfProduction(), weeklyMPS.getProductionType());
    }
    
    public boolean checkForWeek5 (ArrayList<MasterProductionScheduleWeeklyEntity> mps) {
        boolean week = false;
        for (int i = 0; i < mps.size(); i++) {
            if (mps.get(i).getWeeklyWorkingDays2()!=0) {
                week = true;
                break;
            }
        }
        return week;
    }
    
    public long calculateAllQuantity() {
        ArrayList<MasterProductionScheduleDailyEntity> dailyMPS = new ArrayList<MasterProductionScheduleDailyEntity>(current.getMpsDaily());
        MasterProductionScheduleDailyEntity mps = dailyMPS.get(0);
        return dailyFacade.calculateAllQuantity(mps);
    }
    
    public long calculateClassicNuts() {
        ArrayList<MasterProductionScheduleDailyEntity> dailyMPS = new ArrayList<MasterProductionScheduleDailyEntity>(current.getMpsDaily());
        MasterProductionScheduleDailyEntity mps = dailyMPS.get(0);
        return dailyFacade.calculateEachProduct(mps, "Classic Nutri Bar (Nuts)");
    }
    
    public long calculateClassicFruits() {
        ArrayList<MasterProductionScheduleDailyEntity> dailyMPS = new ArrayList<MasterProductionScheduleDailyEntity>(current.getMpsDaily());
        MasterProductionScheduleDailyEntity mps = dailyMPS.get(0);
        return dailyFacade.calculateEachProduct(mps, "Classic Nutri Bar (Fruits)");
    }
    
    public long calculatePremiumNuts() {
        ArrayList<MasterProductionScheduleDailyEntity> dailyMPS = new ArrayList<MasterProductionScheduleDailyEntity>(current.getMpsDaily());
        MasterProductionScheduleDailyEntity mps = dailyMPS.get(0);
        return dailyFacade.calculateEachProduct(mps, "Premium Power (Chocolate Nuts)");
    }
    
    public long calculatePremiumFruits() {
        ArrayList<MasterProductionScheduleDailyEntity> dailyMPS = new ArrayList<MasterProductionScheduleDailyEntity>(current.getMpsDaily());
        MasterProductionScheduleDailyEntity mps = dailyMPS.get(0);
        return dailyFacade.calculateEachProduct(mps, "Premium Power Bar (Redcurrant Fruits)");
    }

}
